<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="../layui/layui.js"></script>
    <script src="../js/jquery-1.8.0.min.js"></script>
    <script src="../js/ajax.js"></script>
    <link rel="stylesheet" href="../layui/css/layui.css">
    <link rel="stylesheet" href="../css/addPage.css">
</head>

<body>
    <form action="" class="layui-form layui-form-pane">
        <input type="hidden" name="goodsId" id="goodsId" placeholder="请输入" autocomplete="off" class="layui-input">
        <div class="layui-form-item">
            <label class="layui-form-label">商品名称</label>
            <div class="layui-input-block">
                <input type="text" name="goodsName" readonly="readonly" id="goodsName" required lay-verify="required"
                    placeholder="请输入" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">销售单价</label>
            <div class="layui-input-block">
                <input type="text" name="price" id="price" oninput="count()" required lay-verify="required"
                    placeholder="请输入" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">销售数量</label>
            <div class="layui-input-block">
                <input type="number" name="number" id="number" oninput="count()" required lay-verify="required"
                    placeholder="请输入" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">销售总额</label>
            <div class="layui-input-block">
                <input type="text" name="totalPrice" id="totalPrice" readonly="readonly" placeholder="请输入"
                    autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">客户</label>
            <div class="layui-input-block">
                <select name="customId" id="customId" lay-verify="required" lay-search="">
                    <option value=""></option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">销售人</label>
            <div class="layui-input-block">
                <select name="operatorId" id="operatorId" lay-verify="required" lay-search="">
                    <option value=""></option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" id="send" lay-submit lay-filter="add">立即提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>
    <script src="../js/host.js"></script>
    <script>

        function count() {
            let price = $("#price").val()
            let number = $("#number").val()
            if (price != "" && number != "") {
                if (!isNaN(price)) {
                    $("#totalPrice").val(parseFloat(price) * number)
                } else {
                    $("#price").val("")
                }
            }
        }

        $(function () {
            layui.use(["table", "form"], function () {
                var table = layui.table;
                var form = layui.form;
                setTimeout(function () {
                    form.render("select")
                }, 50)
                $.ajaxSettings.async = false;

                new ajax(host + "/getCustom", "get", {}, "json", function (data) {
                    $.each(data, function (index, item) {
                        $("#customId").append("<option value='" + item.id + "'>" + item.customName + "</option>")
                    })
                });
                new ajax(host + "/getStaff", "get", {}, "json", function (data) {
                    $.each(data, function (index, item) {
                        $("#operatorId").append("<option value='" + item.id + "'>" + item.staffName + "</option>")
                    })
                });

                form.on("submit(add)", function (data) {
                    new ajax(host + "/addSaleGoods", "post", data.field, "json", function (data) {
                        var index = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(index);
                        parent.layer.msg('提交成功', {
                            icon: 1,
                            time: 2000
                        });
                    });
                    $.ajaxSettings.async = true;
                    return false;
                })

            })
        })
    </script>
</body>

</html>